<?php

namespace App\Http\Controllers\Kms\App\Auth;

use App\Http\Controllers\BaseController;
use App\Http\Validate\Kms\Auth\AuthValidate;
use App\Service\Kms\Auth\AuthService;
use GuzzleHttp\Exception\GuzzleException;
use Illuminate\Contracts\Foundation\Application;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;
use Illuminate\Routing\Redirector;
use Illuminate\Validation\ValidationException;
use Psr\Container\ContainerExceptionInterface;
use Psr\Container\NotFoundExceptionInterface;

class AuthController extends BaseController
{
    /**
     * @param Request $request
     * @return JsonResponse
     * @throws GuzzleException
     * @throws ValidationException
     * @throws ContainerExceptionInterface
     * @throws NotFoundExceptionInterface
     */
    public function fsLogin(Request $request): JsonResponse
    {
        $params = AuthValidate::fsLoginValidate(
            $request->merge([
                "appVersion"         => $request->header("version", ""),
                "userAgent"          => $request->header("user-agent", ""),
                "longitude"          => $request->header("longitude", ""),
                "latitude"           => $request->header("latitude", ""),
                "deviceModel"        => $request->header("deviceModel", ""),
                "deviceSystem"       => $request->header("deviceSystem", ""),
                "deviceSerialNumber" => $request->header("deviceSerialNumber", ""),
            ])->all()
        );
        return $this->success(AuthService::fsLogin($params, $request->ip()));
    }

    /**
     * @param Request $request
     * @return JsonResponse
     * @throws ContainerExceptionInterface
     * @throws NotFoundExceptionInterface
     * @throws ValidationException
     */
    public function virtualLogin(Request $request): JsonResponse
    {
        $params = AuthValidate::virtualLoginValidate(
            $request->merge([
                "appVersion"         => $request->header("version", ""),
                "userAgent"          => $request->header("user-agent", ""),
                "longitude"          => $request->header("longitude", ""),
                "latitude"           => $request->header("latitude", ""),
                "deviceModel"        => $request->header("deviceModel", ""),
                "deviceSystem"       => $request->header("deviceSystem", ""),
                "deviceSerialNumber" => $request->header("deviceSerialNumber", ""),
            ])->all()
        );

        return $this->success(AuthService::virtualLogin($params, $request->ip()));
    }

    /**
     * @param Request $request
     * @return Application|RedirectResponse|Redirector
     * @throws ContainerExceptionInterface
     * @throws NotFoundExceptionInterface
     * @throws ValidationException
     */
    public function code(Request $request)
    {
        $params = AuthValidate::codeValidate($request->all());
        return AuthService::code($params);
    }

    /**
     * @param Request $request
     * @return JsonResponse
     * @throws ContainerExceptionInterface
     * @throws NotFoundExceptionInterface
     * @throws ValidationException
     */
    public function callback(Request $request): JsonResponse
    {
        $params = AuthValidate::callbackValidate($request->all());
        return $this->success((array)$params);
    }

    /**
     * @param Request $request
     * @return JsonResponse
     * @throws ContainerExceptionInterface
     * @throws NotFoundExceptionInterface
     * @throws ValidationException
     */
    public function token(Request $request): JsonResponse
    {
        $params = AuthValidate::tokenValidate($request->all());
        return $this->success(AuthService::token($params));
    }

    /**
     * @return JsonResponse
     */
    public function logout(): JsonResponse
    {
        AuthService::logout();
        return $this->success();
    }

    /**
     * 用户登录注册-审核用
     * @param Request $request
     * @return JsonResponse
     */
    public function login(Request $request)
    {
        $this->validate(
            $request, [
                'mobile' => 'required|string',
                'code'   => 'required|string',
            ]
        );
        $appVersion = $request->header("version", "");
        return $this->success(AuthService::mobileLogin($request->mobile, $request->code, $request->ip(), $appVersion));
    }
}
